﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

using Invoicing.Model.Financial;
namespace Invoicing.Command.Financial
{
    public class BankCommand
    {
        public static BankModel CreateNew(BankModel model)
        {
            if (model != null)
            {
                using (DBCentre db = new DBCentre())
                {
                    SaveCheck(model, db);
                    var data = Convertor.ConvertFrom<Bank, BankModel>(model);
                    db.DataContext.Banks.Add(data);
                    db.DataContext.SaveChanges();
                    model.ID = data.ID;
                }
            }
            return null;
        }

        public static void Update(BankModel model)
        {
            if (model != null && model.ID > 0)
            {
                using (DBCentre db = new DBCentre())
                {
                    var data = db.DataContext.Banks.FirstOrDefault(p => p.ID == model.ID);
                    if (data != null)
                    {
                        SaveCheck(model, db);
                        Convertor.UpdateValue(model, data);
                        db.DataContext.SaveChanges();
                    }
                }
            }
        }

        private static void SaveCheck(BankModel model, DBCentre db)
        {
            if (string.IsNullOrEmpty(model.BankName))
                throw new ApplicationException("请输入名称。");
            if (db != null)
            {
                if (db.DataContext.Banks.Any(p => p.BankName== model.BankName && p.CardNo==model.CardNo && p.ID != model.ID))
                    throw new ApplicationException("重复的记录，请检查。");
            }
        }

        public static BankModel GetModel(int id)
        {
            if (id > 0)
            {
                using (DBCentre db = new DBCentre())
                {
                    var data = db.DataContext.Banks.FirstOrDefault(p => p.ID == id);
                    if (data != null)
                    {
                        var model = Convertor.ConvertTo<BankModel, Bank>(data);
                        return model;
                    }
                }
            }
            return null;
        }

        public static BankCollection GetBanks()
        {
            var result = new BankCollection();
            using (DBCentre db = new DBCentre())
            {
                IQueryable<Bank> q = db.DataContext.Banks;
                q = q.Where(p => p.ID != 7);
                var n = q.Count();
                if (n > 0)
                {
                    q = q.OrderBy(p => p.ID);
                    var lst = q.ToList();
                    result = Convertor.ConvertCollection<BankModel, Bank, BankCollection>(lst);
                    result.TotalRecordsCount = n;
                }
            }
            return result;
        }

        public static void Delete(int id)
        {
            if (id > 0 && id != 7)
            {
                using (DBCentre db = new DBCentre())
                {
                    db.Delete<Bank>(p => p.ID == id);
                    db.DataContext.SaveChanges();
                }
            }
        }
    }
}
